package com.mediacloud.app.newsmodule.view;

import com.mediacloud.app.assembly.util.Utility;
import com.mediacloud.app.assembly.views.CircularImageViewX;
import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.graphics.Paint.Style;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

public class DesktopLayout extends LinearLayout 
{
	public CircularImageViewX controlFloatImg;
	public DesktopLayout(Context context) 
	{
		super(context);
		setOrientation(LinearLayout.VERTICAL);
		this.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		LayoutInflater.from(context).inflate(R.layout.aappfactory_audio_floatcontrolwin, this);
		controlFloatImg=Utility.findViewById(this, R.id.controlFloatImg);
//		this.addView(view);
		try {
			setDefaultDrawable(context);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public void setDefaultDrawable(Context context)
	{
		int size40dp=context.getResources().getDimensionPixelSize(R.dimen.dimen40);
		int width=size40dp;
		int height=size40dp;
		Bitmap bitmap=Bitmap.createBitmap(width, height, Config.ARGB_8888);
		Drawable drawable = new BitmapDrawable(bitmap);
		Canvas canvas = new Canvas(bitmap);
		int color=AppFactoryGlobalConfig.getDefaultImagLoadBackground();
		canvas.drawColor(color, Mode.ADD);
		Paint paint=new Paint();
		paint.setColor(color);
		paint.setFilterBitmap(true);
		paint.setStyle(Style.FILL);
		canvas.drawRect(new Rect(0, 0, width, height), paint);
		Drawable defaultDrawable=AppFactoryGlobalConfig.getDefaultImageLoadDrawable();
		if(defaultDrawable==null)
			defaultDrawable=context.getResources().getDrawable(R.drawable.default_loading);
		Bitmap defaultLoading=((BitmapDrawable)defaultDrawable).getBitmap();
		Matrix matrix=new Matrix();
		float translateX=0.0f,translateY=0.0f;
		if(defaultLoading.getHeight()>height||defaultLoading.getWidth()>width)
		{
			float scaleX=1.0f,scaleY=1.0f;
			float scaleW,scaleH;
			if(defaultLoading.getHeight()>height&&defaultLoading.getWidth()>width)
			{
				scaleY=1.0f*height/defaultLoading.getHeight();
				scaleX=scaleY;
			}
			else if(defaultLoading.getHeight()>height)
			{
				scaleY=1.0f*height/defaultLoading.getHeight();
				scaleX=scaleY;
			}
			else if(defaultLoading.getWidth()>width)
			{
				scaleX=1.0f*width/defaultLoading.getWidth();
				scaleY=scaleX;
			}
			scaleW=defaultLoading.getWidth()*scaleX;
			scaleH=defaultLoading.getHeight()*scaleY;
			translateX=(width-scaleW)/2.0f;
			translateY=(height-scaleH)/2.0f;
			matrix.postScale(scaleX, scaleY);
		}
		else
		{
			translateX=(width-defaultLoading.getWidth())/2.0f;
			translateY=(height-defaultLoading.getHeight())/2.0f;
		}
		matrix.postTranslate(translateX, translateY);
		canvas.drawBitmap(defaultLoading, matrix, paint);
		drawable.draw(canvas);
		controlFloatImg.defaultDrawable=drawable;
		controlFloatImg.defaultBackGroundColor=Color.TRANSPARENT;
		controlFloatImg.setDefaultRes();
	}

}